package com.pet.serve.repository;

import com.pet.serve.entity.Invest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.Date;
import java.util.List;

public interface InvestRepository extends JpaRepository<Invest, Long> {

    // 根据用户名查询充值记录，支持分页
    @Query("SELECT i FROM Invest i WHERE i.username = :username")
    List<Invest> findByUsername(@Param("username") String username);

    // 根据充值状态查询记录
    @Query("SELECT i FROM Invest i WHERE i.status = :status")
    List<Invest> findByStatus(@Param("status") Invest.InvestStatus status);

    // 根据时间范围查询充值记录
    @Query("SELECT i FROM Invest i WHERE i.time BETWEEN :startDate AND :endDate")
    List<Invest> findByTimeBetween(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    // 查询所有记录，支持分页
    @Query("SELECT i FROM Invest i")
    List<Invest> findAllInvests();

    // 获取待审批的充值记录
    @Query("SELECT i FROM Invest i WHERE i.status = 'no'")
    List<Invest> findPendingInvestments();


    // 根据审批账号查询充值记录
    List<Invest> findByApproval(String approval);

    // 根据状态查询充值记录
    List<Invest> findByStatus(String status);

    // 批量更新充值记录的审批状态
    @Query("UPDATE Invest i SET i.status = :status, i.approval = :approval WHERE i.investid IN :ids")
    void updateStatusByIds(@Param("status") String status, @Param("approval") String approval, @Param("ids") List<Long> ids);

    @Query("SELECT i FROM Invest i WHERE (:username IS NULL OR i.username = :username) " +
            "AND (:starttime IS NULL OR i.time >= :starttime) " +
            "AND (:endtime IS NULL OR i.time <= :endtime)")
    Page<Invest> findAllByConditions(@Param("username") String username,
                                     @Param("starttime") Date starttime,
                                     @Param("endtime") Date endtime,
                                     Pageable pageable);

    @Query("SELECT i FROM Invest i WHERE i.status = :status " +
            "AND (:username IS NULL OR i.username = :username) " +
            "AND (:starttime IS NULL OR i.time >= :starttime) " +
            "AND (:endtime IS NULL OR i.time <= :endtime)")
    Page<Invest> findByStatus(@Param("status") Invest.InvestStatus status,
                              @Param("username") String username,
                              @Param("starttime") Date starttime,
                              @Param("endtime") Date endtime,
                              Pageable pageable);

    @Query("SELECT i FROM Invest i WHERE (:username IS NULL OR i.username = :username) " +
            "AND (:approval IS NULL OR i.approval = :approval) " +
            "AND (:starttime IS NULL OR i.time >= :starttime) " +
            "AND (:endtime IS NULL OR i.time <= :endtime)")
    Page<Invest> findAllByFilters(
            @Param("username") String username,
            @Param("approval") String approval,
            @Param("starttime") Date starttime,
            @Param("endtime") Date endtime,
            Pageable pageable);

    @Query("SELECT i FROM Invest i WHERE i.status = :status " +
            "AND (:username IS NULL OR i.username = :username) " +
            "AND (:approval IS NULL OR i.approval = :approval) " +
            "AND (:starttime IS NULL OR i.time >= :starttime) " +
            "AND (:endtime IS NULL OR i.time <= :endtime)")
    Page<Invest> findByStatusAndFilters(
            @Param("status") Invest.InvestStatus status,
            @Param("username") String username,
            @Param("approval") String approval,
            @Param("starttime") Date starttime,
            @Param("endtime") Date endtime,
            Pageable pageable);

}
